package sopt.engSearch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.json.JSONException;
import org.json.JSONObject;

import statics.SoptVars;

import comm.util.SoptHashMap;

public class TranslateProc {
	public static String getTranslatedString(String sourceStr,String langPair){
		String translatedStr = "";
		
		URL url = null;//URL 주소 객체
        URLConnection connection;//URL접속을 가지는 객체
        InputStream is;//URL접속에서 내용을 읽기위한 Stream
        InputStreamReader isr;
        BufferedReader br;
        String result = "";
        
        try{
            //URL객체를 생성하고 해당 URL로 접속한다..
            String requestStr = SoptVars.GOOGLETRANSLATE_URL+
					    		"?v=1.0&key="+SoptVars.GOOGLEAPIKEY+"&q="+URLEncoder.encode(sourceStr,"UTF-8");
            requestStr+="&langpair=";
            if(langPair.equals(SoptVars.KOR_TO_ENG))requestStr+="ko|en";
            else requestStr+="en|ko";
								
            url = new URL(requestStr);
            connection = url.openConnection();
            

            //내용을 읽어오기위한 InputStream객체를 생성한다..
            is = connection.getInputStream();
            
            
            isr = new InputStreamReader(is,"UTF-8");
            br = new BufferedReader(isr);
            
            //내용을 읽어서 화면에 출력한다..
            String buf = null;
            result = "";
            while(true){
                buf = br.readLine();
                if(buf!=null)result+=buf;
                else break;
                
                //System.out.println(buf);
            }
        }catch(MalformedURLException mue){
            System.err.println("잘못되 URL입니다. 사용법 : java URLConn http://hostname/path]");
            System.exit(1);
        }catch(IOException ioe){
            System.err.println("IOException " + ioe);
            ioe.printStackTrace();
            System.exit(1);
        }
        
        JSONObject jsonObject;
        JSONObject resultJson;
		try {
			jsonObject = new JSONObject(result);
			resultJson = jsonObject.getJSONObject("responseData");
	        translatedStr = resultJson.getString("translatedText");
		} catch (JSONException e) {
			translatedStr = sourceStr;
		}
		
		//System.out.println(translatedStr);
		return translatedStr;
	}
	
	public static void chkUrlCorrect(String str){
		URL url = null;//URL 주소 객체
        URLConnection connection;//URL접속을 가지는 객체
        HttpURLConnection httpConnection;
        InputStream is;//URL접속에서 내용을 읽기위한 Stream
        InputStreamReader isr;
        BufferedReader br;
        String result = "";
        
        try{
            //URL객체를 생성하고 해당 URL로 접속한다..
            String requestStr = str;
            
            System.out.println("CONNECTED URL:"+str);
            url = new URL(requestStr);
            
            
            connection = url.openConnection();
            httpConnection = (HttpURLConnection)url.openConnection();
            
            System.out.println("#############여긴 http coneection ");
            System.out.println(httpConnection.getResponseCode());
            System.out.println("========================================요~~");
            System.out.println(HttpURLConnection.HTTP_MOVED_PERM);
            System.out.println(HttpURLConnection.HTTP_MOVED_TEMP);
            
            System.out.println("@@@@@@@@@여긴 connection프로퍼티즈~");
            System.out.println(connection.getRequestProperties().toString());
            
            //내용을 읽어오기위한 InputStream객체를 생성한다..
            is = httpConnection.getInputStream();
            
            
            isr = new InputStreamReader(is,"UTF-8");
            br = new BufferedReader(isr);
            
            //내용을 읽어서 화면에 출력한다..
            String buf = null;
            result = "";
            while(true){
                buf = br.readLine();
                if(buf!=null)result+=buf;
                else break;
                
                //System.out.println(buf);
            }
            if(result.indexOf("<div id=\"DAUMCOMMON_ERROR_MSG\">")>0)
            	System.out.println("느으으으아아아아아!!! 다음 에러 메시지다!!!");
        }catch(MalformedURLException mue){
            System.err.println("잘못되 URL입니다. 사용법 : java URLConn http://hostname/path]");
            System.exit(1);
        }catch(IOException ioe){
            System.err.println("IOException " + ioe);
            ioe.printStackTrace();
            System.exit(1);
        }
        
	}
	
}
